using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using GZ.DB.Entity.wcs_yb;
using GZ.DB.IRepository.wcs_yb;
using GZ.DB.Repository.wcs_yb;
using GZ.Common.Data;

namespace GZ.Projects.WCS_YB
{
	/// <summary>
	/// 任务确认反馈结果
	/// </summary>
	public class TaskConfirmResult
	{
		/// <summary>
		/// 是否成功执行
		/// </summary>
		public bool IsSuccess { get; set; }

		/// <summary>
		/// 执行结果消息
		/// </summary>
		public string Message { get; set; }

		public TaskConfirmResult(bool isSuccess, string message)
		{
			IsSuccess = isSuccess;
			Message = message;
		}
	}

	/// <summary>
	/// 任务确认反馈辅助类
	/// </summary>
	public class TaskConfirmHelper
	{

		/// <summary>
		/// 交互反馈
		/// </summary>
		/// <param name="num">堆垛机编号</param>
		/// <returns>处理结果</returns>
		public TaskConfirmResult InteractionFeedback(string num)
		{
			//单工位堆垛机  任务完成反馈
			//plc回复stb = 1 wcs此时要置位信号变为0
			string suffixName = $"TC{num}";
			string msg = $"\r\n\t[堆垛机任务完成反馈及更新[{suffixName}]]：Start{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
			DateTime startTime = DateTime.Now;
			bool isSuccess = true;
			try
			{
				
				bool result = ConfirmTask_SingleStation("0", out string eMsg);
				if (!result)
				{
					msg += $"\r\n\t[堆垛机完成反馈清除更新异常]：指令清除结果失败{{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}}\" + eMsg;";
				}
				msg += $"\r\n\t[堆垛机任务完成反馈信号清除[{suffixName}]] 信号清除结束，结果为：{result}";

			}
			catch (Exception ex)
			{
				msg += $"\r\n\t[Error]{ex}";
				isSuccess = false;
			}

			msg += $"\r\n\t[EndTime]{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
			msg += $"\r\n\t[SpanTime]{(int)(DateTime.Now - startTime).TotalMilliseconds}" + "ms";
			msg += $"\r\n\t[堆垛机任务置位交互[{suffixName}]]End";
			return new TaskConfirmResult(isSuccess, msg);
		}

		/// <summary>
		/// 正常完成反馈
		/// </summary>
		/// <param name="num">堆垛机编号</param>
		/// <returns>处理结果</returns>
		public TaskConfirmResult NormalFinishFeedback(string num)
		{
			//单工位堆垛机  任务完成反馈
			//plc 1自动完成  wcs 1自动完成
			string suffixName = $"TC{num}";
			bool taskResult = false;
			bool memSend = false;
			bool wcsConfirm = false;
			string msg = $"\r\n\t[[{suffixName}]任务正常完成处理]：Start{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
			DateTime startTime = DateTime.Now;
			bool isSuccess = true;
			try
			{
				//查找是否存在已下发的任务，堆垛机接收指令的托盘号是否正确
				string plcTaskNo = Conn.YBRedis.GetValue($"{suffixName}.dev_taskNo");
				string memDeviceTaskNo = Conn.YBRedis.GetValue($"MEM.{suffixName}_DeviceTaskNo"); //mem记录的设备任务号
				//string memCont = Conn.YBRedis.GetValue($"MEM.{suffixName}_ContNo"); //mem记录的任务条码
				IStackerTaskRepository stackerRepo = new StackerTaskRepository(); //处理数据库
				IMainMissionRepository mainMissionRepo = new MainMissionRepository();

				DBOpeation dbOpeation = new DBOpeation();
				
				if (plcTaskNo == "0" && memDeviceTaskNo == "0" )
				{
					msg += $"\r\n\t[堆垛机信息完成异常]：堆垛机的任务号为0无法查询到任务信息";
					taskResult = ClearTask_SingleStation(null, out string eMsg);
					wcsConfirm = ConfirmTask_SingleStation("1", out string confirmErr);
					memSend = StackMemTaskInfo("0", "0", "0", "0", num);
					msg += $"\r\n\t[堆垛机信息完成异常]：清除下发的任务信号结果：{taskResult}，回复堆垛机信号结果：{wcsConfirm}，重置MEM内存信号结果{memSend}";
					if (!taskResult || !wcsConfirm)
					{
						msg += $"\r\n\t[堆垛机信息完成异常]：指令清除结果失败{{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}}\" + eMsg;";
					}
					return new TaskConfirmResult(false,msg);
				}

				//后续考虑用plc的值
				StackerTaskEntity stackerTask = stackerRepo
					.FindList(t =>
					          (t.TaskState == "Send" || t.TaskState == "Doing") && t.DeviceNo == suffixName &&
					          t.DeviceTaskNo == memDeviceTaskNo).OrderByDescending(t => t.CreateTime).LastOrDefault();
				//如果查找出来的不是空 记一下日志 并且输出
				if (stackerTask != null)
				{
					msg += $"\r\n\t任务编号{stackerTask.TaskNo}，设备任务号为{stackerTask.DeviceTaskNo}，任务条码{stackerTask.TrayCode}的任务完成{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";

					//如果是入库和移库任务
					if (stackerTask.TaskType == "ST_In" || stackerTask.TaskType == "ST_Move")
					{
						var mainEntity = mainMissionRepo
							.FindList(t => t.device_task_no == stackerTask.DeviceTaskNo &&
							          t.tray_code == stackerTask.TrayCode).OrderByDescending(t => t.CreateTime)
							.LastOrDefault();
						if (mainEntity != null)
						{
							//修改数据库 状态为完成
							stackerTask.TaskState = "Finish";
							stackerTask.TaskEndTime = DateTime.Now;
							stackerTask.LastUpdatedTime = DateTime.Now;

							mainEntity.MissionState = "Finish";
							mainEntity.InteStationNo = "Finish";
							mainEntity.MissionEndTime = DateTime.Now;
							mainEntity.LastUpdatedTime = DateTime.Now;
							//todo 接口上报任务状态
							var systemSynchronization = new SystemSynchronizationEntity
							{
								syn_direction = 2,
								syn_status = 0,
								ope_type = "2",  //任务完成
								order_no = mainEntity.device_task_no,
								tray_code = mainEntity.tray_code,
								task_no = mainEntity.task_no,
								ope_time = DateTime.Now,
								cur_pos = mainEntity.end_position,
								err_cnt = 0
							};
							bool resultMs = dbOpeation.UpdateMainAndStackInsertSysTask(mainEntity, stackerTask,
							                                                           systemSynchronization, out string err);
							msg += $"\r\n\t任务编号{stackerTask.TaskNo}，设备任务号为{stackerTask.DeviceTaskNo}，任务条码{stackerTask.TrayCode}的任务更新完成，结果为{resultMs}{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
							if (resultMs)
							{
								wcsConfirm = ConfirmTask_SingleStation("1", out string confirmErr);
								memSend = StackMemTaskInfo("0", "0", "0", "0", num);
								msg += $"\r\n\t[任务完成信号回复及清除MEM记录值],确认信号回复结果{wcsConfirm}，清除MEM结果{memSend}{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
								if(!wcsConfirm || !memSend)
								{
									return new TaskConfirmResult(false,msg);
								}
							}
							else
							{
								msg += $"\r\n\t[堆垛机信息完成异常]：数据库更新失败{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}" + err;
							}
						}
						else
						{
							msg += $"\r\n\t[堆垛机信息完成异常]：主任务号{stackerTask.DeviceTaskNo}无法查询到主任务信息,请检查主任务状态{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
						}
					}
					else if (stackerTask.TaskType == "ST_Out")
					{
						//出库任务
						MainMissionEntity mainEntity = mainMissionRepo
							.FindList(t => t.device_task_no == stackerTask.DeviceTaskNo &&
							          t.tray_code == stackerTask.TrayCode).OrderByDescending(t => t.CreateTime)
							.LastOrDefault();
						if (mainEntity != null)
						{
							stackerTask.TaskState = "Finish";
							stackerTask.TaskEndTime = DateTime.Now;
							stackerTask.LastUpdatedTime = DateTime.Now;

							bool resultMt = dbOpeation.UpdateStackerTask(stackerTask, out string err1);
							msg += $"\r\n\t任务编号{stackerTask.TaskNo}，设备任务号为{stackerTask.DeviceTaskNo}，任务条码{stackerTask.TrayCode}的出库任务完成，数据处理结果为{resultMt}{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
							if (resultMt)
							{
								wcsConfirm = ConfirmTask_SingleStation("1", out string confirmErr);
								memSend = StackMemTaskInfo("0", "0", "0", "0", num);
								msg += $"\r\n\t[任务完成信号回复及清除MEM记录值],确认信号回复结果{wcsConfirm}，清除MEM结果{memSend}{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
								if(!wcsConfirm || !memSend)
								{
									return new TaskConfirmResult(false,msg);
								}
							}
							else
							{
								msg += $"\r\n\t[堆垛机信息完成异常]：数据库更新失败{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
							}
						}
						else
						{
							msg +=
								$"\r\n\t[堆垛机信息完成异常]：未查询到主任务信息关于{stackerTask.DeviceTaskNo}的设备任务号，请检查主任务{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
						}
					}
				}
				else
				{
					msg += $"\r\n\t[堆垛机信息完成异常]：堆垛机任务号{plcTaskNo}无法查询到任务信息，此时MEM内存任务号为{memDeviceTaskNo}";
				}
			}
			catch (Exception ex)
			{
				msg += $"\r\n\t[Error]{ex}";
				isSuccess = false;
			}

			msg += $"\r\n\t[EndTime]{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
			msg += $"\r\n\t[SpanTime]{(int)(DateTime.Now - startTime).TotalMilliseconds}" + "ms";
			msg += $"\r\n\t[{suffixName}]任务正常完成]End";
			return new TaskConfirmResult(isSuccess, msg);
		}

		/// <summary>
		/// 强制完成反馈
		/// </summary>
		/// <param name="num">堆垛机编号</param>
		/// <returns>处理结果</returns>
		public TaskConfirmResult ForceFinishFeedback(string num)
		{
			//单工位堆垛机  任务完成反馈
			//plc 2强制完成  wcs 2 强制完成
			string suffixName = $"TC{num}";
			bool memSend = false;
			bool taskResult = false;
			bool wcsConfirm = false;
			string msg = $"\r\n[[{suffixName}]任务正常完成处理]：Start{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
			DateTime startTime = DateTime.Now;
			bool isSuccess = true;
			try
			{
				//查找是否存在已下发的任务，堆垛机接收指令的托盘号是否正确
				string plcTaskNo = Conn.YBRedis.GetValue($"{suffixName}.dev_taskNo");
				string memDeviceTaskNo = Conn.YBRedis.GetValue($"MEM.{suffixName}_DeviceTaskNo"); //mem记录的设备任务号
				//string memCont = Conn.YBRedis.GetValue($"MEM.{suffixName}_ContNo"); //mem记录的任务条码
				IStackerTaskRepository stackerRepo = new StackerTaskRepository(); //处理数据库
				IMainMissionRepository mainMissionRepo = new MainMissionRepository();

				
				if (plcTaskNo == "0" && memDeviceTaskNo == "0" )
				{
					msg += $"\r\n\t[堆垛机信息完成异常]：堆垛机工位1的任务号为空无法查询到任务信息";
					taskResult = ClearTask_SingleStation(null, out string eMsg);
					wcsConfirm = ConfirmTask_SingleStation("2",out string confirmErr);
					memSend = StackMemTaskInfo("0", "0", "0", "0", num);
					msg += $"\r\n\t[堆垛机信息完成异常]：清除下发的任务信号结果：{taskResult}，回复堆垛机信号结果：{wcsConfirm}，重置MEM内存信号结果{memSend}";
					if (!taskResult || !wcsConfirm)
					{
						msg += $"\r\n\t[堆垛机信息完成异常]：指令清除结果失败{{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}}\" + eMsg;";
					}
					return new TaskConfirmResult(false,msg);
				}
				DBOpeation dbOpeation = new DBOpeation();
				
				var stackerTask = stackerRepo
					.FindList(t =>
					          (t.TaskState == "Send" || t.TaskState == "Doing") && t.DeviceNo == suffixName &&
					          t.DeviceTaskNo == memDeviceTaskNo).OrderByDescending(t => t.CreateTime).LastOrDefault();
				//如果查找出来的不是空 记一下日志 并且输出
				if (stackerTask != null)
				{
					//任务完成，还要把变量组的mem清除一下
					msg += $"\r\n\t任务编号{stackerTask.TaskNo}，设备任务号为{stackerTask.DeviceTaskNo}，任务条码{stackerTask.TrayCode}的任务强制完成{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
					//如果是入库和移库任务
					if (stackerTask.TaskType == "ST_In" || stackerTask.TaskType == "ST_Move")
					{
						var mainEntity = mainMissionRepo
							.FindList(t => t.device_task_no == stackerTask.DeviceTaskNo &&
							          t.tray_code == stackerTask.TrayCode).OrderByDescending(t => t.CreateTime)
							.LastOrDefault();
						if (mainEntity != null)
						{
							//修改数据库 状态为完成
							stackerTask.TaskState = "Finish";
							stackerTask.TaskEndTime = DateTime.Now;
							stackerTask.LastUpdatedTime = DateTime.Now;

							mainEntity.MissionState = "Finish";
							mainEntity.InteStationNo = "Finish";
							mainEntity.MissionEndTime = DateTime.Now;
							mainEntity.LastUpdatedTime = DateTime.Now;
							//todo 接口上报任务状态
							var systemSynchronization = new SystemSynchronizationEntity
							{
								syn_direction = 2,
								syn_status = 0,
								ope_type = "2",  //任务完成
								order_no = mainEntity.device_task_no,
								tray_code = mainEntity.tray_code,
								task_no = mainEntity.task_no,
								ope_time = DateTime.Now,
								cur_pos = mainEntity.end_position,
								err_cnt = 0
							};
							bool resultMs = dbOpeation.UpdateMainAndStackInsertSysTask(mainEntity, stackerTask,
							                                                           systemSynchronization, out string err);
							msg += $"\r\n\t任务编号{stackerTask.TaskNo}，设备任务号为{stackerTask.DeviceTaskNo}，任务条码{stackerTask.TrayCode}的任务更新完成，结果为{resultMs}{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
							if (resultMs)
							{
								wcsConfirm = ConfirmTask_SingleStation("2",out string confirmErr);
								memSend = StackMemTaskInfo("0", "0", "0", "0", num);
								msg += $"\r\n\t[任务完成信号回复及清除MEM记录值],确认信号回复结果{wcsConfirm}，清除MEM结果{memSend}{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
								if(!wcsConfirm || !memSend)
								{
									return new TaskConfirmResult(false,msg);
								}
							}
							else
							{
								msg += $"\r\n\t[堆垛机信息完成异常]：数据库更新失败{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}" + err;
							}
						}
						else
						{
							msg += $"\r\n\t[堆垛机信息完成异常]：主任务号{stackerTask.DeviceTaskNo}无法查询到主任务信息,请检查主任务状态{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
						}
					}
					else if (stackerTask.TaskType == "ST_Out")
					{
						//出库任务
						MainMissionEntity mainEntity = mainMissionRepo
							.FindList(t => t.device_task_no == stackerTask.DeviceTaskNo &&
							          t.tray_code == stackerTask.TrayCode).OrderByDescending(t => t.CreateTime)
							.LastOrDefault();
						if (mainEntity != null)
						{
							stackerTask.TaskState = "Finish";
							stackerTask.TaskEndTime = DateTime.Now;
							stackerTask.LastUpdatedTime = DateTime.Now;

							bool resultMt = dbOpeation.UpdateStackerTask(stackerTask, out string err1);
							msg += $"\r\n\t任务编号{stackerTask.TaskNo}，设备任务号为{stackerTask.DeviceTaskNo}，任务条码{stackerTask.TrayCode}的出库任务完成，数据处理结果为{resultMt}{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
							if (resultMt)
							{
								wcsConfirm = ConfirmTask_SingleStation("2", out string confirmErr);
								memSend = StackMemTaskInfo("0", "0", "0", "0", num);
								msg += $"\r\n\t[任务完成信号回复及清除MEM记录值],确认信号回复结果{wcsConfirm}，清除MEM结果{memSend}{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
								if(!wcsConfirm || !memSend)
								{
									return new TaskConfirmResult(false,msg);
								}
							}
							else
							{
								msg += $"\r\n\t[堆垛机信息完成异常]：数据库更新失败{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
							}
						}
						else
						{
							msg +=
								$"\r\n\t[堆垛机信息完成异常]：未查询到主任务信息关于{stackerTask.DeviceTaskNo}的设备任务号，请检查主任务{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
						}
					}
				}
			}
			catch (Exception ex)
			{
				msg += $"\r\n\t[Error]{ex}";
				isSuccess = false;
			}

			msg += $"\r\n\t[EndTime]{DateTime.Now:yyyy/MM/dd HH:mm:ss}";
			msg += $"\r\n\t[SpanTime]{(int)(DateTime.Now - startTime).TotalMilliseconds}" + "ms";
			msg += $"\r\n\t[{suffixName}]任务正常完成]End";
			return new TaskConfirmResult(isSuccess, msg);
		}

		/// <summary>
		/// 取消任务反馈
		/// </summary>
		/// <param name="num">堆垛机编号</param>
		/// <returns>处理结果</returns>
		public TaskConfirmResult CancelFeedback(string num)
		{
			//单工位堆垛机  任务完成反馈
			//plc taskstatus1=3 正常完成 wcs ConfirmSignal1 3任务完成确认
			//plc 5强制完成  wcs 10 强制完成
			//plc 6取消任务  wcs  8 任务删除
			string suffixName = $"TC{num}";
			string msg = $"\r\n[[{suffixName}]取消任务处理]：Start{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
			DateTime startTime = DateTime.Now;
			bool taskResult = false;
			bool wcsConfirm = false;
			bool clearTask = false;
			bool memSend = false;
			bool isSuccess = true;
			try
			{
				//查找是否存在已下发的任务，堆垛机接收指令的托盘号是否正确
				string plcTaskNo = Conn.YBRedis.GetValue($"{suffixName}.dev_taskNo");
				string memDeviceTaskNo = Conn.YBRedis.GetValue($"MEM.{suffixName}_DeviceTaskNo"); //mem记录的设备任务号
				//string memCont = Conn.YBRedis.GetValue($"MEM.{suffixName}_ContNo"); //mem记录的任务条码
				IStackerTaskRepository stackerRepo = new StackerTaskRepository(); //处理数据库
				IMainMissionRepository mainMissionRepo = new MainMissionRepository();

				if (plcTaskNo == "0" && memDeviceTaskNo == "0" )
				{
					msg += $"\r\n\t[堆垛机信息完成异常]：堆垛机工位1的任务号为0无法查询到任务信息";
					taskResult = ClearTask_SingleStation(null, out string eMsg);
					wcsConfirm = ConfirmTask_SingleStation("3",out string confirmErr);
					memSend = StackMemTaskInfo("0", "0", "0", "0", num);
					msg += $"\r\n\t[堆垛机信息完成异常]：清除下发的任务信号结果：{taskResult}，回复堆垛机信号结果：{wcsConfirm}，重置MEM内存信号结果{memSend}";
					if (!taskResult || !wcsConfirm)
					{
						msg += $"\r\n\t[堆垛机信息完成异常]：指令清除结果失败{{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}}\" + eMsg;";
					}
					return new TaskConfirmResult(false,msg);
				}
				DBOpeation dbOpeation = new DBOpeation();
				var stackerTask = stackerRepo
					.FindList(t =>
					          (t.TaskState == "Send" || t.TaskState == "Doing") && t.DeviceNo == suffixName &&
					          t.DeviceTaskNo == memDeviceTaskNo).OrderByDescending(t => t.CreateTime).LastOrDefault();
				//如果查找出来的不是空 记一下日志 并且输出
				if (stackerTask != null)
				{
					
					msg += $"\r\n\t查找任务任务编号{stackerTask.TaskNo}，设备任务号为{stackerTask.DeviceTaskNo}，任务条码{stackerTask.TrayCode}开始取消任务{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
					//修改数据库 状态为Cancel
					stackerTask.TaskState = "Cancel";
					stackerTask.TaskEndTime = DateTime.Now;
					stackerTask.LastUpdatedTime = DateTime.Now;

					bool result = dbOpeation.UpdateStackerTask(stackerTask, out string error);

					//主任务也要取消
					var mainEntity = mainMissionRepo
						.FindList(t => t.task_no == stackerTask.TaskNo)
						.OrderByDescending(t => t.CreateTime).LastOrDefault();
					if (mainEntity != null)
					{
						mainEntity.MissionState = "Cancel";
						mainEntity.MissionEndTime = DateTime.Now;
						mainEntity.LastUpdatedTime = DateTime.Now;
						//接口
						var systemSynchronization = new SystemSynchronizationEntity
						{
							syn_direction = 2,
							syn_status = 0,
							ope_type = "7",  //任务完成
							order_no = mainEntity.device_task_no,
							tray_code = mainEntity.tray_code,
							task_no = mainEntity.task_no,
							ope_time = DateTime.Now,
							cur_pos = mainEntity.end_position,
							err_cnt = 0
						};
						bool resultMs =
							dbOpeation.UpdateMainAndInsertSyn(mainEntity, systemSynchronization, out string err);
						msg += $"\r\n\t任务编号{stackerTask.TaskNo}，设备任务号为{stackerTask.DeviceTaskNo}，任务条码{stackerTask.TrayCode}的出库任务完成，数据处理结果为{resultMs}{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
						//任务都发送取消成功，还要把变量组的mem清除一下
						//三个参数，哪台堆垛机 命令类型 确认信号
						wcsConfirm = ConfirmTask_SingleStation("3",out string errmsg);
						memSend = StackMemTaskInfo("0", "0", "0", "0", num);
						msg += $"\r\n\t[任务完成信号回复及清除MEM记录值],确认信号回复结果{wcsConfirm}，清除MEM结果{memSend}{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
						if(!wcsConfirm || !memSend)
						{
							return new TaskConfirmResult(false,msg);
						}
					}
					else
					{
						wcsConfirm = ConfirmTask_SingleStation("3",out string errmsg);
						memSend = StackMemTaskInfo("0", "0", "0", "0", num);
						msg += $"\r\n\t获取主任务异常，上报接口失败，回复堆垛机取消任务确认结果{wcsConfirm}{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
						isSuccess = false;
					}
				}
				else
				{
					clearTask = ClearTask_SingleStation(null, out string ermsg);
					wcsConfirm = ConfirmTask_SingleStation("3",out string errmsg);
					memSend = StackMemTaskInfo("0", "0", "0", "0", num);
					msg += $"\r\n\t获取堆垛机任务异常，请检查状态或者条码，回复堆垛机取消任务确认结果{wcsConfirm},置位MEM信号{memSend}{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
					isSuccess = false;
				}
			}
			catch (Exception ex)
			{
				msg += $"\r\n\t[Error]{ex}";
				isSuccess = false;
			}

			msg += $"\r\n\t[EndTime]{DateTime.Now:yyyy/MM/dd HH:mm:ss:fff}";
			msg += $"\r\n\t[SpanTime]{(int)(DateTime.Now - startTime).TotalMilliseconds}" + "ms";
			msg += $"\r\n[{suffixName}]取消任务结束]End";
			return new TaskConfirmResult(isSuccess, msg);
		}

		/// <summary>
		/// 堆垛机内存任务记录
		/// </summary>
		/// <param name="deviceTaskNo">设备任务编号</param>
		/// <param name="contNum">托盘编码</param>
		/// <param name="curNum">当前位置</param>
		/// <param name="toNum">目标位置</param>
		/// <param name="num">堆垛机编号</param>
		/// <returns>是否成功</returns>
		public bool StackMemTaskInfo(string deviceTaskNo, string contNum, string curNum, string toNum, string num)
		{
			try
			{
				string setQueue = $"MEMQueue";
				string groupName = $"MEM.TC{num}_INFO";

				GZ.Modular.Redis.WriteGroupEntity group = new GZ.Modular.Redis.WriteGroupEntity
				{
					groupName = groupName,
					queueStatus = 1,
					queueTime = DateTime.Now,
					writeList = new List<GZ.Modular.Redis.ParamData>
					{
						new GZ.Modular.Redis.ParamData
						{
							paramName = $"MEM.TC{num}_DeviceTaskNo",
							paramValue = deviceTaskNo
						},
						new GZ.Modular.Redis.ParamData
						{
							paramName = $"MEM.TC{num}_ContNo",
							paramValue = contNum
						},
						new GZ.Modular.Redis.ParamData
						{
							paramName = $"MEM.TC{num}_CurPos",
							paramValue = curNum
						},
						new GZ.Modular.Redis.ParamData
						{
							paramName = $"MEM.TC{num}_ToPos",
							paramValue = toNum
						}
					}
				};
				bool result = Conn.YBRedis.SetQueue(group, setQueue, "", "");
				return result;
			}
			catch (Exception e)
			{
				Conn.YBLog.Error(1, $"[堆垛机MEM记录失败[TC{num}]]", 1, e.ToString());
				return false;
			}
		}
		
		//清除任务
		public bool ClearTask_SingleStation(StackerTaskEntity task, out string err)
		{
			
			err = "";
			try
			{
				string groupName = $"TC01.SendTask";
				string setQueue = $"TC01Queue";
				string contType = $"TC01.wcs_trayType";
				string srcZ = $"TC01.wcs_startPosX";
				string srcX = $"TC01.wcs_startPosY";
				string srcY = $"TC01.wcs_startPosZ";
				string destZ = $"TC01.wcs_endPosX";
				string destX = $"TC01.wcs_endPosY";
				string destY = $"TC01.wcs_endPosZ";
				string taskType = $"TC01.wcs_taskType";
				string taskNo = $"TC01.wcs_taskNo";
				string taskExe = $"TC01.wcs_taskExe";  //1下发 2数据异常

				GZ.Modular.Redis.WriteGroupEntity group = new GZ.Modular.Redis.WriteGroupEntity
				{
					groupName = groupName,
					queueStatus = 1,
					queueTime = DateTime.Now,
					writeList = new List<GZ.Modular.Redis.ParamData>
					{
						new GZ.Modular.Redis.ParamData { paramName = contType, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = srcZ, paramValue = "0" }, //排
						new GZ.Modular.Redis.ParamData { paramName = srcX, paramValue = "0" }, //列
						new GZ.Modular.Redis.ParamData { paramName = srcY, paramValue = "0" }, //层
						new GZ.Modular.Redis.ParamData { paramName = destZ, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = destX, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = destY, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = taskType, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = taskNo, paramValue = "0" },
						new GZ.Modular.Redis.ParamData { paramName = taskExe, paramValue = "0" }

					}
				};
				return Conn.YBRedis.SetQueue(group, setQueue, "", "");
			}
			catch (Exception ex)
			{
				err += ex.ToString();
				return false;
			}

		}
		
		//确认信号
		public bool ConfirmTask_SingleStation(string confirmValue, out string err)
		{
			
			err = "";
			try
			{
				string groupName = $"TC01.StackerFinishSend";
				string setQueue = $"TC01Queue";
				string wcsTaskFinishAffirm = $"TC01.wcs_taskFinishAffirm";  //1下发 2数据异常

				GZ.Modular.Redis.WriteGroupEntity group = new GZ.Modular.Redis.WriteGroupEntity
				{
					groupName = groupName,
					queueStatus = 1,
					queueTime = DateTime.Now,
					writeList = new List<GZ.Modular.Redis.ParamData>
					{
						new GZ.Modular.Redis.ParamData { paramName = wcsTaskFinishAffirm, paramValue = confirmValue }
					}
				};
				return Conn.YBRedis.SetQueue(group, setQueue, "", "");
			}
			catch (Exception ex)
			{
				err += ex.ToString();
				return false;
			}

		}
	}
}